package com.skp.shaphan.ui.dialogs;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;

import com.skp.job.ProjectJobException;
import com.skp.shaphan.ShaphanApp;
import com.skp.shaphan.internalStorage.DBConnector;
import com.skp.shaphan.internalStorage.Fields;
import com.skp.shaphan.internalStorage.Tables;
import com.skp.shaphan.savedConnections.SavedConnections;
import com.skp.shaphan.ui.buttons.ButtonsPane;
import com.skp.shaphan.ui.buttons.DevOptionsOkButton;
import com.skp.shaphan.ui.buttons.DialogCancelButton;
import com.skp.shaphan.ui.connectioncontrols.SavedConnectionComboBox;

/**
 * This dialog allows a user to configure the development options
 * @author Stephen Phillips
 *
 */
public class DevOptionsDialog extends JDialog {
	private JCheckBox enableDev;
	private JCheckBox autoLogin;
	private SavedConnectionComboBox autoConnection;
	
	/**
	 * Create the dialog GUI
	 */
	public DevOptionsDialog() {
		super(ShaphanApp.getMainWindow());
		
        final JFXPanel fxPanel = new JFXPanel();
        this.add(fxPanel);
        this.setSize(600, 400);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("Development Options");

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
            	Parent root;
            	try {
					URL location = getClass().getResource("/com/skp/shaphan/ui/dialogs/DevDialog.fxml");
					FXMLLoader fxmlloader = new FXMLLoader(location);
					DevOptions controller = new DevOptions();
					controller.attachFrame(DevOptionsDialog.this);
					fxmlloader.setController(controller);
					root = (Parent)fxmlloader.load();
					Scene scene = new Scene(root);
			        fxPanel.setScene(scene);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            //initFX(fxPanel);
            }
       });        
	}
	
	/**
	 * Loads the values from the database (Internal Storage)
	 * @throws SQLException
	 */
	public void loadFromDb() throws SQLException {
		Connection conn = DBConnector.getInstance().getConnection();
		
		autoConnection.refreshList();
		
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery("select * from " + Tables.Dev);
		if(rs.next()) {
			if(rs.getString(Fields.Dev_Enabled).equalsIgnoreCase("Y")) {
				enableDev.setSelected(true);
			} else {
				enableDev.setSelected(false);
			}
			if(rs.getString(Fields.Dev_AutoLogin).equalsIgnoreCase("Y")) {
				autoLogin.setSelected(true);
			} else {
				autoLogin.setSelected(false);
			}
			int connId = rs.getInt(Fields.Dev_AutoConnectionId);
			autoConnection.setSelectedConnection(connId);
		}
		rs.close();
		st.close();
	}

	/**
	 * Save the settings from the screen into the Database (Internal Storage)
	 * @throws SQLException
	 */
	public void saveToDb() throws SQLException {
		Connection conn = DBConnector.getInstance().getConnection();
		Statement st = conn.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.FETCH_FORWARD);
		ResultSet rs = st.executeQuery("select * from " + Tables.Dev);
		if(rs.next()) {
			if(enableDev.isSelected()) {
				rs.updateString(Fields.Dev_Enabled, "Y");
			} else {
				rs.updateString(Fields.Dev_Enabled, "N");
			}
			if(autoLogin.isSelected()) {
				rs.updateString(Fields.Dev_AutoLogin, "Y");
			} else {
				rs.updateString(Fields.Dev_AutoLogin, "N");
			}
			rs.updateInt(Fields.Dev_AutoConnectionId, autoConnection.getSelectedConnection());
			rs.updateRow();
		}
		rs.close();
		st.close();		
	}
}
